<?php
class ProductsController extends AppController {
	var $name = 'Products';
    var $category = array(
        1 => 'ATP Zeta Series',
        2 => 'ATP Zeta Series Package',
        3 => 'Energy Series',
        4 => 'Agape Beauty Series',
        5 => 'Agape Online Tools'
    );

    public function admin_listData ($type=null) {
        $rs = $this->Product->listing($this->params['form']);

        $json = ($rs) ? $rs : array(
            'page'  => 1,
            'total' => 0,
            'rows'  => array()
        );

        $this->response($json);
    } // admin_listData

    public function admin_update_status () {
        $json = array('status'=>1);
        $form = $this->params['form'];

        if ($form['task'] == 'delete') {
            $status = 'D';
            $action = 'delete';
        } else {
            $json = array(
                'status' => false,
                'error'  => 'Process failure, missing required data.'
            );
            $this->response($json);
            return;
        }

        if (empty($form['id'])) {
            $json = array(
                'status' => false,
                'error'  => 'Couldn\'t '.$action.' product, missing required record Id.'
            );
            $this->response($json);
            return;
        }

        $ids = explode(',', $form['id']);

        $rs = $this->Product->updateAll(
            array('Product.status' => '"'.$status.'"'),
            array('Product.id' => $ids)
        );

        if ($rs) {
            $json = array('status' => true);
        } else {
            $json = array(
                'status' => false,
                'error'  => 'Fail to '.$action.' product at this time.'
            );
        }

        $this->response($json);
    } // admin_update_status

    public function admin_update () {
        $this->data['Product'] = $this->params['form'];

        if ($this->data) {
            $valid = true;

            foreach ($this->data['Product'] as $fieldname=>$value) {
                if (empty($value) && $value != '0') {
                    $this->data['Product'][$fieldname] = null;
                }
            }

            $this->Product->set($this->data);

            if ($this->Product->validates() && $valid) { // data validated!
                if ($this->Product->save($this->data)) {
                    $mesg = empty($this->params['form']['id']) ? __('New product has been created successfully!',true) : __('Product has been updated successfully!',true);
                    $json = array(
                        'status' => true,
                        'mesg'   => $mesg
                    );
                } else {
                    $mesg = empty($this->params['form']['id']) ? __('Fail insert record to database at this time, please try again.',true) : __('Fail update record to database at this time, please try again.',true);
                    $err  = ($this->error) ? $this->error : $mesg;
                    $json = array(
                        'status' => false,
                        'mesg'   => $err
                    );
                }
            } else { // invalid data found
                $json = array(
                    'status' => false,
                    'mesg'   => __('Please enter the requested details in the fields pointed out below and try again!', true)
                );
                foreach ($this->Product->invalidFields() as $k=>$v) {
                    $json['error'][] = array('id'=>$k, 'field'=>$k, 'code'=>$v);
                }
            }
        } else {
            $json = array(
                'status' => false,
                'mesg'   => __('no data posted', true)
            );
        }

        $this->response($json);
    } // admin_update

    public function admin_get ($id) {
        $this->data = $this->Product->findById($id);

        if (empty($this->data['Product'])) {
            $json = array(
                'status' => false,
                'mesg'   => __('No data found from database', true)
            );
        }

        $data = array();
        $i    = 0;
        foreach ($this->data['Product'] as $f=>$v) {
            $data[$i] = array(
                'f' => $f,
                'v' => $v
            );
            $i++;
        }

        $json = array(
            'status' => true,
            'data'   => $data
        );

        $this->response($json);
    } // admin_get

    public function admin_index () {
        $data = $this->Product->find('list', array(
            'conditions' => array(
                'include_ids' => null,
                'status'      => 'A'
            ),
            'recursive'  => -1,
            'fields'     => 'code, name',
            'order'      => 'code ASC'
        ));
        $products = array();
        foreach ($data as $code=>$name) {
            $products[] = array(
                'code' => $code,
                'name' => $name
            );
        }

        $this->stylesheet  = am($this->stylesheet, 'flexigrid/flexigrid', 'autocomplete', 'popup');
        $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'autocomplete.min', 'flexigrid.min', 'admin_product');
        $this->pageTitle   = __('Products Management', true);

        $this->set('pageTitle', $this->pageTitle);
        $this->set('products', $products);
        $this->set('category', $this->category);
        $this->render('admin_manage');
    } // admin_index
} // ProductsController
?>